package com.learning.jquizenglish.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;

import com.learning.jquizenglish.common.controller.JQuizHistory;
import com.learning.jquizenglish.model.ActivityResultData;
import com.learning.jquizenglish.model.JQuizObserver;
import com.learning.jquizenglish.screen.MainScreen;
import com.learning.jquizenglish.screen.profile.ProfilePreference;
import com.learning.jquizenglish.utility.CameraUtils;

public class MainActivity extends BaseAudioPlayerActivity {


	/** The screen width. */
	public static int SCREEN_WIDTH;
	
	/** The screen height. */
	public static int SCREEN_HEIGHT;
	
	private MainScreen mainScreen;
	private JQuizObserver mProfileListener;
	
	public static final int DO_QUIZ_REQUEST_CODE = 1111;
	public static final int PICK_CAMERA_REQUEST_CODE = 2222;
	public static final int PICK_FROM_FILE_REQUEST_CODE = 3333;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initScreenSize();
		mainScreen = new MainScreen();
		switchContent(mainScreen, false);
	}
	
	public MainScreen getMainScreen(){
		return mainScreen;
	}
	
	/**
	 * Inits the screen size.
	 */
	@SuppressLint("NewApi")
	protected void initScreenSize(){
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			Display display = getWindowManager().getDefaultDisplay();
			Point size = new Point();
			display.getSize(size);
			SCREEN_WIDTH = size.x;
			SCREEN_HEIGHT = size.y;
		} else {
			Display display = getWindowManager().getDefaultDisplay();
			SCREEN_WIDTH = display.getWidth();
			SCREEN_HEIGHT = display.getHeight();
		}
	}
	
	public void registerProfileListener(JQuizObserver profileListener){
		mProfileListener = profileListener;
	}

	public void updateProfile(Object ob){
		if (mProfileListener!=null) {
			mProfileListener.onUpdate(ob);
		}
	}
	
	public void startDoQuizActivityForResult(Intent intent){
		startActivityForResult(intent, DO_QUIZ_REQUEST_CODE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == DO_QUIZ_REQUEST_CODE) {
				updateProfile(null);
			}else if (requestCode == PICK_CAMERA_REQUEST_CODE
					|| requestCode == PICK_FROM_FILE_REQUEST_CODE) {
				if (mProfileListener!=null) {
					mProfileListener.onUpdate(new ActivityResultData(requestCode, resultCode, data));
				}
			}
		}
	}
	
	
	@Override
	protected void onDestroy() {
		ProfilePreference.getInstance().setProfile(null);
		JQuizHistory.getInstance().clearData();
		super.onDestroy();
	}
	

}
